package game;

import java.awt.Point;
import java.util.List;
import java.util.ArrayList;

/**
 * Creates Pawn's that are required by the chess game.
 * 
 * @author Nicole Waldrum
 * @date January 30, 2010
 * @version 1.0
 * 
 * @author Mark Kelly
 * @date February 19, 2010
 * @version 2.0
 * 
 * @author Trevor Bastian
 * @date February 20, 2010
 * @version 2.0
 */
public class Pawn extends Piece {
	/**
	 * Creates a new instance of Pawn
	 * 
	 * @param name the name of this Pawn
	 * @param position the initial position of this Pawn
	 * @param team the team that this Pawn is on
	 */
	public Pawn(String name, Point position, Team team) {
		super(name, position, team);
	}
	
	/**
	 * Creates a new instance of Pawn
	 * 
	 * @param position the initial position of the Pawn
	 * @param team the team that the Pawn is on
	 */
	public Pawn(Point position, Team team) {
		this((team == Team.WHITE) ? "WP" : "BP", position, team);
	}

	/**
	 * Assumes that 'isValidMove()' has been called and returns true first
	 * {@inheritDoc}
	 */
	@Override
	public List<Point> checkMovePattern(Point dest) {
		Point start = this.getLocation();
		Team team = this.getTeam();

		List<Point> ret = new ArrayList<Point>();
		Point p;

		if (!this.hasMoved()) {	// check for the move two forward on its first move
			if ((int)Math.abs(start.getY() - dest.getY()) == 2) {
				if ( team == Team.WHITE ) {
					p = new Point((int)start.getX(), (int)(start.getY() + 1));
					ret.add(p);
					ret.add(dest);
					return ret;
				} else if ( team == Team.BLACK ) {
					p = new Point((int)start.getX(), (int)(start.getY() - 1));
					ret.add(p);
					ret.add(dest);
					return ret;
				}
			}
		}
		ret.add(dest);
		return ret;
	}

	/**
	 * Checks if the pawn is making a valid move forward: Does not check for diagonal moving, dealt with in
	 * classicChess isValidMove method...
	 * {@inheritDoc}
	 */
	@Override
	public boolean isValidMove(Piece piece, Point dest) {
		Point start = piece.getLocation();
		Team team = piece.getTeam();
		
		// check if its moving forward and not sideways or diagonal
		if ( start.getX() == dest.getX() ) {
			// check if its moving forward and not backward
			if (( team == Team.BLACK ) && (int)( start.getY() - dest.getY()) == 1 ) {
				return super.isValidMove(piece, dest);
			} else if ( team == Team.WHITE && (int)(dest.getY() - start.getY()) == 1 ) {
				return super.isValidMove(piece, dest);
			}else if (!piece.hasMoved()) {	// check for the move two forward on its first move
				if ((int)Math.abs(start.getY() - dest.getY()) == 2) {
					return super.isValidMove(piece, dest);
				}
			}
		}
		return false;
	}
}
